package com.yuanian.ecsplus.converter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuanian.dp.common.vo.CustomeFieldValueVo;
import com.yuanian.ecsplus.exception.EcsPlusException;
import com.yuanian.ecsplus.context.ReflectContext;

/**
 * CustomeFieldValueVo字段的处理类
 */
public class CustomerFieldValueVoConverter implements TypeConverter {

    @Override
    public Object obj2Map(ReflectContext reflectContext) {
        if (reflectContext.getObject() == null || reflectContext.getField() == null) {
            return null;
        }
        try {
            //获取字段的值
            Object fieldValue = reflectContext.getRawResult();
            if (fieldValue != null) {
                CustomeFieldValueVo CustomeFieldValueVo = (CustomeFieldValueVo)fieldValue;
                Object customerFieldValue = CustomeFieldValueVo.getFieldValue();
                //多值引用处理
                if (customerFieldValue != null && customerFieldValue instanceof JSONArray) {
                    String result = "";
                    JSONArray array = (JSONArray)customerFieldValue;
                    for (int i = 0; i < array.size(); i++) {
                        JSONObject jsonObject = (JSONObject) array.get(i);
                        if (i < array.size() - 1) {
                            result = result + jsonObject.getString("objectId") + ",";
                        } else {
                            result = result + jsonObject.getString("objectId");
                        }

                    }
                    //BOMRefValueBean valueBean = new BOMRefValueBean();
                    //valueBean.setValue(result);
                    return result;
                } else if (customerFieldValue != null && customerFieldValue instanceof JSONObject) {//单值引用
                    return ((JSONObject)customerFieldValue).getString("objectId");
                } else {
                    return customerFieldValue;
                }
            }
            return null;
        } catch (IllegalAccessException e) {
            throw new EcsPlusException(e.getCause());
        }
    }

    @Override
    public Object map2Obj(ReflectContext reflectContext) {
        if (reflectContext.getMap() == null || reflectContext.getField() == null) {
            return null;
        }
        //获取map中字段名对应的值
        Object mapObj = reflectContext.getMapData();
        if (mapObj == null) {
            return null;
        }
        try {
            Object fieldValue = reflectContext.getRawResult();
            //实体类已经有此值，更新
            if (fieldValue != null) {
                CustomeFieldValueVo CustomeFieldValueVo = (CustomeFieldValueVo)fieldValue;
                CustomeFieldValueVo.setFieldValue(mapObj);
                return CustomeFieldValueVo;
            } else {//实体类字段没有值
                if (mapObj instanceof CustomeFieldValueVo) {
                    return mapObj;
                } else {
                    throw new EcsPlusException(reflectContext.getField().getType() + ":" + mapObj.getClass() + "类型不兼容");
                   /* CustomeFieldValueVo customeFieldValueVo = new CustomeFieldValueVo();
                    customeFieldValueVo.setFieldValue(mapObj);
                    return customeFieldValueVo;*/
                }
            }
        } catch (IllegalAccessException e) {
            throw new EcsPlusException(e.getCause());
        }
    }
}
